<?php
/**
 *
 * (model)
 */
namespace App\Models\Product;

use App\Models\Basics\AmazonAccount;
use App\Models\Basics\AmazonCountry;
use App\Models\Basics\SysCountry;
use App\Models\Basics\SysPlatAccount;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;
class ProductAmazonSku extends Model
{
    protected $table = 'product_amazon_sku';

    protected $guarded = [];

    public static $_fanEu = [
        1 => '泛欧',
        0 => '非泛欧'
    ];

    public static $_barcodeType = [
        1 => 'UPC码',
        2 => 'EAN码',
        3 => '无',
    ];

    const STATUS_YES = 1; //产品启用
    const STATUS_NO  = 2; //产品停用
    public static $_status = [
        self::STATUS_YES => '启用',
        self::STATUS_NO  => '停用',
    ];

    public static function displayStatus() {
        return ['1' => '<i class="fa fa-check text-green"></i>', '2' => '<i class="fa fa-close text-red"></i>'];
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function account(){
        return $this->belongsTo(SysPlatAccount::class,'account_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class,'country_id');
    }

    public function classify(){
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
            );
    }

    public static function getModelById($id){
        return self::query()->find($id);
    }

    public static function getModelByCode($sellerSku){
        return self::query()->where('seller_sku',$sellerSku)->first();
    }

    public static function getValueByCode($sellerSku,$value){
        $v = self::query()->where('seller_sku',$sellerSku)->value($value);
        return $v ?: '';
    }

    public static function getPskuIdBySellerSku($sellerSku){
        $pskuId = self::query()->where('seller_sku',$sellerSku)->value('psku_id');
        return $pskuId ?: 0;
    }

    /**
     * 标记泛欧状态
     * @param $sellerSku
     * @param $childAsin
     * @param $fnSku
     * @param $countryIdsForFanEU
     * @param $notInAccountId
     */
    public static function upTimes($sellerSku,$childAsin,$fnSku,$countryIdsForFanEU,$notInAccountId){
        $query = self::query()
            ->where('status',1)
            ->whereIn('country_id',$countryIdsForFanEU)
            ->whereNotIn('account_id',$notInAccountId)
            ->where('seller_sku',$sellerSku)
            ->where('child_asin',$childAsin)
            ->where('fnsku',$fnSku);
        $_fanEuQuery = clone $query;
        $fanEuCount = $_fanEuQuery->where('fan_eu',1)->count();
        if($fanEuCount > 0){
            $_query = clone $query;
            $count = $_query->count();
            $query->update(['fan_eu' => 1,'times' => $count]);
        }else{
            $query->update(['fan_eu' => 0,'times' => 0]);
        }
    }

    public static function getTimesCountry($sellerSku,$childAsin,$fnSku){
        $country = AmazonCountry::options(['fan_eu' => 1]);
        $countryIdsForFanEU = array_keys($country->toArray());
        $notInAccountId = AmazonAccount::$_notFanEu;
        $res = self::query()
            ->where('status',1)
            ->whereIn('country_id',$countryIdsForFanEU)
            ->whereNotIn('account_id',$notInAccountId)
            ->where('seller_sku',$sellerSku)
            ->where('child_asin',$childAsin)
            ->where('fnsku',$fnSku)
            ->pluck('country_id');
        $country = SysCountry::getCountryByCountryIds($res)->toArray();
        return $country;
    }

    public static function getDownloadData($res)
    {
        foreach ($res as $model){
            $temp = [
                $model->sku->psku_code ?? $model->psku_id,
                $model->sku->psku_cn ?? '',
                ProductClassify::$_season[$model->skuClassify->season ?? 3],
                $model->skuClassify->title ?? '',
                $model->skuClassify->model_no ?? '',
                $model->account->account ?? '',
                $model->country->country ?? '',
                $model->seller_sku,
                User::getUsernameById($model->seller_id),
                ProductAmazonSku::$_barcodeType[$model->barcode_type],
                $model->barcode,
                $model->sku->part_number ?? '',
                $model->amazon_part_number,
                $model->parent_asin,
                $model->child_asin,
                $model->fnsku,
                ProductAmazonSku::$_fanEu[$model->fan_eu],
                $model->times == 0 || $model->times == 4 ? '正常' : $model->times,
                User::getUsernameById($model->cuser_id),
                $model->created_at,
                ProductAmazonSku::$_status[$model->status],
            ];
            yield $temp;
        }
    }

    /**
     * 获取获取亚马逊产品
     * @param null $psku_id
     * @param null $status
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function getProductAmazonSkuList($psku_id = null, $status = null){
        $query = self::query();
        if ($psku_id) {
            $query->where('psku_id', $psku_id);
        }
        if ($status) {
            $query->where('status', $status);
        }

        return $query->get();
    }

    /**
     * 获取获取亚马逊产品(个数)
     * @param null $id
     * @param null $psku_id
     * @param null $status
     * @return int
     */
    public static function getProductAmazonSkuCount($id = null, $psku_id = null, $status = null){
        $query = self::query();
        if ($id) {
            $query->where('id', '<>', $id);
        }
        if ($psku_id) {
            $query->where('psku_id', $psku_id);
        }
        if ($status) {
            $query->where('status', $status);
        }

        return $query->count();
    }

}
